package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.resource.property.jaxb.Properties;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Generates a table containing fuel types and (non-virtual) sources using it.
 * 
 * @author Alex Kan and Kelvin Green
 * @version 2.0
 */
public class WattDepotFuelTypesCommand extends WattDepotCommand {

  /**
   * Generates a table containing fuel types and (non-virtual) sources using it.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string of requested fuelType table data.
   */
  public String doCommand(String[] args) {
    //Check for correct input
    if (args.length < 2 || args.length > 2) {
      return "fueltypes {source}";
    }
    
    return getFuelTypes(args);
  }

  /**
   * Creates a table containing fuel types and (non-virtual) sources using it.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string variable named output containing all the output for the method.
   */
  private String getFuelTypes(String[] args) {
    String output = ""; // Holds output for command.
    Source source = null; // Holds Source specified by user.
    boolean error = false;
    final String strLSFO = "LSFO";
    final String strCoal = "coal";
    final String strWaste = "waste";
    if (args.length < 2 || args.length > 2) { // Check for correct input
      return "Correct command is fueltypes {source}";
    }
    else {
      try {
        source = getClient().getSource(args[1]);
        if (source.isVirtual()) {
          ArrayList<String> waste = new ArrayList<String>();
          ArrayList<String> lsfo = new ArrayList<String>();
          ArrayList<String> coal = new ArrayList<String>();
          if (source.isSetSubSources()) {
            List<String> subSources = source.getSubSources().getHref();
            String subSource = "";
            String fuelType = "";
            for (int i = 0; i < subSources.size(); i++) {
              String[] subSourceArray = subSources.get(i).split("/");
              subSource = subSourceArray[subSourceArray.length - 1];
              if (getClient().getSource(subSource).isVirtual()) {
                Source current = getClient().getSource(subSource);
                subSources.addAll(current.getSubSources().getHref());
                continue;
              }
              fuelType = getClient().getSource(subSource).getProperty("fuelType");

              if (fuelType.equals(strWaste)) {
                waste.add(subSource);
              }
              else if (fuelType.equals(strCoal)) {
                coal.add(subSource);
              }
              else if (fuelType.equals(strLSFO)) {
                lsfo.add(subSource);
              }
            }

            ListIterator<String> liWaste = waste.listIterator();
            ArrayList<String> outputArray = new ArrayList<String>();
            outputArray.add("waste: ");
            while (liWaste.hasNext()) {
              outputArray.add(liWaste.next() + ", ");
            }
            outputArray.add(cr);
            output += outputArray.toString() + cr;
            output = output.replace("[", "");
            output = output.replace("]", "");
            output = output.replace(",", "");
            outputArray = new ArrayList<String>();

            ListIterator<String> liCoal = coal.listIterator();
            outputArray.add("coal: ");
            while (liCoal.hasNext()) {
              outputArray.add(liCoal.next() + ", ");
            }
            outputArray.add(cr);
            output += outputArray.toString() + cr;
            output = output.replace("[", "");
            output = output.replace("]", "");
            output = output.replace(",", "");
            outputArray = new ArrayList<String>();

            ListIterator<String> liLSFO = lsfo.listIterator();
            outputArray.add("LSFO: ");
            while (liLSFO.hasNext()) {
              outputArray.add(liLSFO.next() + ", ");
            }
            output += outputArray.toString() + cr;
            output = output.replace("[", "");
            output = output.replace("]", "");
            output = output.replace(",", "");
          }
        }
        else {
          Properties prop = source.getProperties();
          return source.getName() + ": " + prop.getProperty("fuelType");

        }
      }
      catch (MiscClientException mce) {
        error = true;
      }
      catch (NotAuthorizedException e) {
        error = true;
        if (error) {
          return "Unspecified error. Please contact support";
        }
      }
      catch (ResourceNotFoundException e) {
        error = true;
        return "Source does not exist.";
      }
      catch (BadXmlException e) {
        error = true;
      }
    }

    return output;
  }
}
